<template>
  <div class="image-to-video-page">
    <div class="page-header">
      <h1 class="page-title">图生视频</h1>
      <p class="page-description">上传静态图片，让AI为您生成精彩的动态视频内容</p>
    </div>

    <div class="main-content">
      <!-- 左侧控制面板 -->
      <div class="control-panel">
        <div class="control-section">
          <h3 class="section-title">上传图片</h3>
          <div class="upload-area" @click="triggerUpload">
            <input 
              ref="fileInput" 
              type="file" 
              accept="image/*" 
              @change="handleImageUpload"
              style="display: none"
            />
            <div v-if="!sourceImage" class="upload-placeholder">
              <div class="upload-icon">🎬</div>
              <p>点击上传静态图片</p>
              <span class="upload-hint">支持 JPG、PNG 格式</span>
            </div>
            <div v-else class="uploaded-image">
              <img :src="sourceImage" alt="源图片" class="source-img" />
              <button class="remove-btn" @click.stop="removeImage">❌</button>
            </div>
          </div>
        </div>

        <div class="control-section">
          <h3 class="section-title">视频设置</h3>
          <div class="settings-group">
            <div class="setting-item">
              <label class="setting-label">视频时长</label>
              <div class="duration-options">
                <button 
                  v-for="duration in videoDurations" 
                  :key="duration.id"
                  :class="['duration-btn', { active: selectedDuration === duration.id }]"
                  @click="selectDuration(duration.id)"
                >
                  {{ duration.name }}
                </button>
              </div>
            </div>

            <div class="setting-item">
              <label class="setting-label">动画风格</label>
              <div class="style-grid">
                <button 
                  v-for="style in animationStyles" 
                  :key="style.id"
                  :class="['style-btn', { active: selectedStyle === style.id }]"
                  @click="selectStyle(style.id)"
                >
                  <span class="style-icon">{{ style.icon }}</span>
                  <span class="style-name">{{ style.name }}</span>
                </button>
              </div>
            </div>

            <div class="setting-item">
              <label class="setting-label">运动强度</label>
              <div class="slider-group">
                <input 
                  v-model="motionIntensity" 
                  type="range" 
                  min="0" 
                  max="100" 
                  class="setting-slider"
                />
                <span class="slider-value">{{ motionIntensity }}%</span>
              </div>
            </div>

            <div class="setting-item">
              <label class="setting-label">视频质量</label>
              <div class="quality-options">
                <button 
                  v-for="quality in videoQualities" 
                  :key="quality.id"
                  :class="['quality-btn', { active: selectedQuality === quality.id }]"
                  @click="selectQuality(quality.id)"
                >
                  {{ quality.name }}
                </button>
              </div>
            </div>
          </div>
        </div>

        <div class="control-section">
          <h3 class="section-title">提示词 (可选)</h3>
          <div class="prompt-area">
            <textarea
              v-model="prompt"
              :disabled="isGenerating"
              placeholder="描述您希望的动画效果，例如：微风轻抚，花瓣飘落，阳光透过树叶洒下斑驳的光影..."
              class="prompt-textarea"
              rows="4"
            ></textarea>
          </div>
        </div>

        <div class="control-section">
          <div class="action-controls">
            <button 
              class="action-btn primary large" 
              @click="generateVideo" 
              :disabled="!sourceImage || isGenerating"
            >
              {{ isGenerating ? '生成中...' : '开始生成视频' }}
            </button>
            <button class="action-btn" @click="clearAll" :disabled="!sourceImage && generatedVideos.length === 0">
              清空重置
            </button>
            <button class="action-btn" @click="downloadAll" :disabled="generatedVideos.length === 0">
              批量下载
            </button>
          </div>
        </div>
      </div>

      <!-- 右侧预览区域 -->
      <div class="preview-area">
        <div class="result-container">
          <h3 class="result-title">生成结果</h3>
          
          <div v-if="!isGenerating && generatedVideos.length === 0" class="empty-state">
            <div class="empty-icon">🎥</div>
            <p>上传图片，开始AI视频生成之旅</p>
          </div>

          <div v-if="isGenerating" class="generating-state">
            <div class="generating-animation">
              <div class="frame-sequence">
                <div class="frame">📷</div>
                <div class="frame">📷</div>
                <div class="frame">📷</div>
              </div>
              <div class="video-icon">🎬</div>
            </div>
            <p class="generating-text">AI正在为您的图片注入生命力...</p>
            <div class="progress-bar">
              <div class="progress-fill" :style="{ width: progress + '%' }"></div>
            </div>
            <span class="progress-text">{{ progress }}%</span>
            <div class="progress-steps">
              <span :class="['step', { active: progress >= 25 }]">🔍 分析图片</span>
              <span :class="['step', { active: progress >= 50 }]">🎯 生成关键帧</span>
              <span :class="['step', { active: progress >= 75 }]">🎨 渲染动画</span>
              <span :class="['step', { active: progress >= 100 }]">✅ 完成合成</span>
            </div>
          </div>

          <div v-if="generatedVideos.length > 0" class="videos-grid">
            <div 
              v-for="(video, index) in generatedVideos" 
              :key="index"
              class="video-card"
            >
              <div class="video-wrapper">
                <div class="video-placeholder">
                  <div class="play-button">▶️</div>
                  <div class="video-info">
                    <span class="duration">{{ selectedDuration }}s</span>
                    <span class="quality">{{ selectedQuality }}</span>
                  </div>
                </div>
                <div class="video-overlay">
                  <button class="overlay-btn" @click="playVideo(video, index)">
                    ▶️ 播放
                  </button>
                  <button class="overlay-btn" @click="downloadVideo(video, index)">
                    📥 下载
                  </button>
                </div>
              </div>
              <div class="video-meta">
                <span class="video-title">生成视频 {{ index + 1 }}</span>
                <span class="video-style">{{ getStyleName(selectedStyle) }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ImageToVideo',
  data() {
    return {
      sourceImage: null,
      prompt: '',
      isGenerating: false,
      progress: 0,
      generatedVideos: [],
      selectedDuration: 3,
      selectedStyle: 'natural',
      selectedQuality: 'HD',
      motionIntensity: 75,
      
      videoDurations: [
        { id: 3, name: '3秒' },
        { id: 5, name: '5秒' },
        { id: 10, name: '10秒' },
        { id: 15, name: '15秒' }
      ],
      
      animationStyles: [
        { id: 'natural', name: '自然运动', icon: '🌿' },
        { id: 'cinematic', name: '电影感', icon: '🎬' },
        { id: 'dreamy', name: '梦幻效果', icon: '✨' },
        { id: 'dynamic', name: '动感活力', icon: '⚡' },
        { id: 'vintage', name: '复古风格', icon: '📽️' },
        { id: 'futuristic', name: '科技感', icon: '🔮' }
      ],
      
      videoQualities: [
        { id: 'HD', name: 'HD' },
        { id: '2K', name: '2K' },
        { id: '4K', name: '4K' }
      ]
    }
  },
  methods: {
    triggerUpload() {
      this.$refs.fileInput.click();
    },
    
    handleImageUpload(event) {
      const file = event.target.files[0];
      if (file) {
        const reader = new FileReader();
        reader.onload = (e) => {
          this.sourceImage = e.target.result;
          this.generatedVideos = [];
        };
        reader.readAsDataURL(file);
      }
    },
    
    removeImage() {
      this.sourceImage = null;
      this.generatedVideos = [];
      this.$refs.fileInput.value = '';
    },
    
    selectDuration(duration) {
      this.selectedDuration = duration;
    },
    
    selectStyle(styleId) {
      this.selectedStyle = styleId;
    },
    
    selectQuality(quality) {
      this.selectedQuality = quality;
    },
    
    async generateVideo() {
      if (!this.sourceImage) return;
      
      this.isGenerating = true;
      this.progress = 0;
      this.generatedVideos = [];
      
      // 模拟AI生成进度
      const progressInterval = setInterval(() => {
        this.progress += Math.random() * 8;
        if (this.progress >= 92) {
          this.progress = 92;
          clearInterval(progressInterval);
        }
      }, 400);
      
      // 模拟AI处理过程
      setTimeout(() => {
        clearInterval(progressInterval);
        this.progress = 100;
        
        // 生成模拟视频
        this.generatedVideos = [
          {
            id: 1,
            url: 'mock_video_1.mp4',
            thumbnail: 'https://via.placeholder.com/400x300/FF6B6B/FFFFFF?text=动态视频1'
          },
          {
            id: 2,
            url: 'mock_video_2.mp4',
            thumbnail: 'https://via.placeholder.com/400x300/4ECDC4/FFFFFF?text=动态视频2'
          }
        ];
        
        this.isGenerating = false;
      }, 6000);
    },
    
    clearAll() {
      this.sourceImage = null;
      this.generatedVideos = [];
      this.prompt = '';
      this.progress = 0;
      this.$refs.fileInput.value = '';
    },
    
    playVideo(video, index) {
      // 模拟播放视频
      console.log(`播放视频 ${index + 1}:`, video);
    },
    
    downloadVideo(video, index) {
      // 模拟下载视频
      console.log(`下载视频 ${index + 1}:`, video);
    },
    
    downloadAll() {
      this.generatedVideos.forEach((video, index) => {
        setTimeout(() => {
          this.downloadVideo(video, index);
        }, index * 500);
      });
    },
    
    getStyleName(styleId) {
      const style = this.animationStyles.find(s => s.id === styleId);
      return style ? style.name : '自然运动';
    }
  }
}
</script>

<style scoped>
.image-to-video-page {
  padding: 1.5rem;
  min-height: 100vh;
  background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
}

.page-header {
  text-align: center;
  margin-bottom: 2rem;
}

.page-title {
  font-size: 2.5rem;
  font-weight: bold;
  color: #2d3748;
  margin-bottom: 1rem;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.page-description {
  font-size: 1.1rem;
  color: #4a5568;
  max-width: 600px;
  margin: 0 auto;
}

.main-content {
  display: flex;
  gap: 2rem;
  max-width: 1400px;
  margin: 0 auto;
}

.control-panel {
  width: 350px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  height: fit-content;
}

.control-section {
  margin-bottom: 2rem;
}

.section-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: #2d3748;
  margin-bottom: 1rem;
}

.upload-area {
  border: 2px dashed #a8edea;
  border-radius: 12px;
  padding: 1.5rem;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #f0fdfa;
  position: relative;
}

.upload-area:hover {
  border-color: #38b2ac;
  background-color: #e6fffa;
}

.upload-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.upload-icon {
  font-size: 2.5rem;
  margin-bottom: 0.5rem;
}

.upload-hint {
  font-size: 0.8rem;
  color: #a0aec0;
}

.uploaded-image {
  position: relative;
  display: flex;
  justify-content: center;
}

.source-img {
  max-width: 100%;
  max-height: 200px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.remove-btn {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: none;
  background: #e53e3e;
  color: white;
  font-size: 0.8rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.settings-group {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.setting-item {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.setting-label {
  font-size: 0.9rem;
  font-weight: 500;
  color: #4a5568;
}

.duration-options {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

.duration-btn {
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 0.875rem;
  font-weight: 500;
}

.duration-btn:hover {
  border-color: #38b2ac;
  background-color: #f0fdfa;
}

.duration-btn.active {
  border-color: #38b2ac;
  background-color: #e6fffa;
  color: #2d7f7b;
}

.style-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

.style-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  gap: 0.25rem;
}

.style-btn:hover {
  border-color: #38b2ac;
  background-color: #f0fdfa;
}

.style-btn.active {
  border-color: #38b2ac;
  background-color: #e6fffa;
  color: #2d7f7b;
}

.style-icon {
  font-size: 1.25rem;
}

.style-name {
  font-size: 0.75rem;
  font-weight: 500;
}

.quality-options {
  display: flex;
  gap: 0.5rem;
}

.quality-btn {
  padding: 0.5rem 1rem;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 0.875rem;
  font-weight: 500;
  flex: 1;
  text-align: center;
}

.quality-btn:hover {
  border-color: #38b2ac;
  background-color: #f0fdfa;
}

.quality-btn.active {
  border-color: #38b2ac;
  background-color: #e6fffa;
  color: #2d7f7b;
}

.slider-group {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.setting-slider {
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: #e2e8f0;
  outline: none;
}

.slider-value {
  font-size: 0.875rem;
  font-weight: 600;
  color: #38b2ac;
  min-width: 45px;
}

.prompt-textarea {
  width: 100%;
  padding: 1rem;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 0.95rem;
  line-height: 1.6;
  resize: vertical;
  transition: all 0.3s ease;
  background: white;
}

.prompt-textarea:focus {
  outline: none;
  border-color: #38b2ac;
  box-shadow: 0 0 0 3px rgba(56, 178, 172, 0.1);
}

.action-controls {
  display: grid;
  gap: 0.75rem;
}

.action-btn {
  padding: 0.875rem 1rem;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-btn.primary {
  background: linear-gradient(135deg, #38b2ac 0%, #4fd1c7 100%);
  color: white;
}

.action-btn.primary.large {
  padding: 1rem 1.5rem;
  font-size: 1.1rem;
}

.action-btn.primary:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(56, 178, 172, 0.4);
}

.action-btn:not(.primary) {
  background: rgba(255, 255, 255, 0.9);
  color: #4a5568;
  border: 1px solid #e2e8f0;
}

.action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.preview-area {
  flex: 1;
}

.result-container {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 2rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.result-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: #2d3748;
  margin-bottom: 1.5rem;
  text-align: center;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400px;
  color: #a0aec0;
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 1rem;
}

.generating-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400px;
  gap: 1.5rem;
}

.generating-animation {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.frame-sequence {
  display: flex;
  gap: 0.5rem;
}

.frame {
  font-size: 2rem;
  animation: frame-flip 1.5s ease-in-out infinite;
}

.frame:nth-child(2) { animation-delay: 0.3s; }
.frame:nth-child(3) { animation-delay: 0.6s; }

.video-icon {
  font-size: 3rem;
  animation: bounce 2s ease-in-out infinite;
}

@keyframes frame-flip {
  0%, 100% { transform: rotateY(0deg); opacity: 0.3; }
  50% { transform: rotateY(180deg); opacity: 1; }
}

@keyframes bounce {
  0%, 20%, 50%, 80%, 100% { transform: translateY(0); }
  40% { transform: translateY(-10px); }
  60% { transform: translateY(-5px); }
}

.generating-text {
  font-weight: 600;
  color: #2d3748;
}

.progress-bar {
  width: 300px;
  height: 8px;
  background: #e2e8f0;
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #38b2ac, #4fd1c7);
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 0.9rem;
  font-weight: 600;
  color: #38b2ac;
}

.progress-steps {
  display: flex;
  gap: 1rem;
  margin-top: 1rem;
}

.step {
  font-size: 0.8rem;
  color: #a0aec0;
  transition: color 0.3s ease;
}

.step.active {
  color: #38b2ac;
  font-weight: 600;
}

.videos-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.5rem;
}

.video-card {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  background: white;
}

.video-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}

.video-wrapper {
  position: relative;
}

.video-placeholder {
  width: 100%;
  height: 200px;
  background: linear-gradient(135deg, #e2e8f0 0%, #cbd5e0 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.play-button {
  font-size: 3rem;
  color: #4a5568;
}

.video-info {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  display: flex;
  gap: 0.5rem;
}

.duration, .quality {
  padding: 0.25rem 0.5rem;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 500;
}

.video-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.video-card:hover .video-overlay {
  opacity: 1;
}

.overlay-btn {
  padding: 0.5rem 1rem;
  background: rgba(255, 255, 255, 0.9);
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  transition: all 0.3s ease;
}

.overlay-btn:hover {
  background: white;
  transform: scale(1.05);
}

.video-meta {
  padding: 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.video-title {
  font-weight: 600;
  color: #2d3748;
}

.video-style {
  font-size: 0.875rem;
  color: #718096;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .main-content {
    flex-direction: column;
  }
  
  .control-panel {
    width: 100%;
  }
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }
  
  .style-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .videos-grid {
    grid-template-columns: 1fr;
  }
  
  .progress-steps {
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
  }
}
</style> 
